

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Result;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.sax.SAXTransformerFactory;
    import javax.xml.transform.sax.TransformerHandler;
    import javax.xml.transform.stream.StreamResult;

    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.AttributesImpl;

    public class SaxToXmlDemo {
        public static void main(String[] args) {
            // 创建一个SAXTransformerFactory
            SAXTransformerFactory stff = (SAXTransformerFactory) SAXTransformerFactory
                    .newInstance();
            try {
                // 获取TransformerHandler对象
                TransformerHandler handler = stff.newTransformerHandler();
                // 通过handler创建Transformers对象
                Transformer former = handler.getTransformer();
                // 通过Transformer对象对生成的xml文件进行设置
                former.setOutputProperty(OutputKeys.INDENT, "yes");
                former.setOutputProperty(OutputKeys.ENCODING, "utf-8");
                // 创建一个result对象，用于进行文件的创建和文件输出流的编写,并使其与handler对象关联
                File f = new File("sax.xml");
                if (!f.exists()) {
                    f.createNewFile();
                }
                Result rs = new StreamResult(new FileOutputStream(f));
                handler.setResult(rs);
                // 利用handler对象进行xml内容的编写
                // 打开doument
                handler.startDocument();
                // 创建Attribute对象，用户重复利用
                AttributesImpl attrs = new AttributesImpl();
                handler.startElement("", "", "class", attrs);
                attrs.clear();
                attrs.addAttribute("", "", "id", "", "1");
                // 创建person标签
                handler.startElement("", "", "person", attrs);
                attrs.clear();
                // 创建name标签
                handler.startElement("", "", "name", attrs);
                String name = "小明";
                handler.characters(name.toCharArray(), 0, name.length());
                handler.endElement("", "", "name");
                handler.endElement("", "", "person");
                handler.endElement("", "", "class");
                // 关闭document
                handler.endDocument();
            } catch (TransformerConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }